var urltest = sessionStorage.getItem("urltest");
var userId = sessionStorage.getItem("userId");
var fullname = sessionStorage.getItem("fullname");
var urlo = '';
$(function() {
	if(userId == "" || userId == undefined || userId == null) {
		top.location.href = "../login.html";
	} else {
		wuye_name();
		datePickerInit();
	}
	var urllll = urltest +"/woke/public/accountChecking";
	$("#formID").attr("action",urllll);
});
//加载物业名称
function wuye_name(data) {
	var parentid = data;
	var reque = function() {
		return {
			parentid: parentid,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/property/selectOrganizeNameList',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				//								console.log(msg);
				console.log(msg.returnString);
				var bb = JSON.parse(msg.returnString);
				//				var str = "";
				var str = '<option value="">' + "请选择物业名称" + '</option>';
				for(var i = 0; i < bb.length; i++) {
					str += "<option value=" + bb[i].organizeid + ">" + bb[i].shortname + "</option>";
				}
				$("#select_wuye").html("");
				$("#select_wuye").append(str);
				$("#select_wuye1").html("");
				$("#select_wuye1").append(str);

			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}
//物业改变查小区
$("#select_wuye").change(function() {
	var scdd_shxz = $("#select_wuye").val();
	if(scdd_shxz == '') {
		var str = '<option value="">' + "请选择小区名称" + '</option>';
		$("#select_xiaoqu").html("");
		$("#select_xiaoqu").append(str);
	} else {
		xiaoqu_name(scdd_shxz)
	}
})
//物业改变查小区
$("#select_wuye1").change(function() {
	var scdd_shxz = $("#select_wuye1").val();
	if(scdd_shxz == '') {
		var str = '<option value="">' + "请选择小区名称" + '</option>';
		$("#select_xiaoqu1").html("");
		$("#select_xiaoqu1").append(str);
	} else {
		xiaoqu_name1(scdd_shxz)
	}
})
//加载小区名称
function xiaoqu_name1(data) {
	var parentid = data;
	var reque = function() {
		return {
			merchantid: parentid,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	if(parentid != "") {
		$.ajax({
			contentType: "application/json; charset=utf-8",
			type: "POST",
			url: urltest + 'verify/property/selectAreaName',
			data: req3,
			async: true,
			dataType: "json",
			success: function(msg) {
				//			console.log(msg);
				if(msg.responseCode == 200) {
					//				console.log(msg);
					console.log(msg.returnString);
					var bb = JSON.parse(msg.returnString);
					//				var str = "";
					var str = '<option value="">' + "请选择小区名称" + '</option>';
					for(var i = 0; i < bb.length; i++) {
						str += "<option value=" + bb[i].areaid + ">" + bb[i].areaname + "</option>";
					}
					$("#select_xiaoqu1").html("");
					$("#select_xiaoqu1").append(str);
				} else {
					alert(msg.resoponseDisp)
				}
			},
			error: function() {
				alert("获取信息失败");
			}
		});
	}

}
//加载小区名称
function xiaoqu_name(data) {
	var parentid = data;
	var reque = function() {
		return {
			merchantid: parentid,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	if(parentid != "") {
		$.ajax({
			contentType: "application/json; charset=utf-8",
			type: "POST",
			url: urltest + 'verify/property/selectAreaName',
			data: req3,
			async: true,
			dataType: "json",
			success: function(msg) {
				//			console.log(msg);
				if(msg.responseCode == 200) {
					//				console.log(msg);
					console.log(msg.returnString);
					var bb = JSON.parse(msg.returnString);
					//				var str = "";
					var str = '<option value="">' + "请选择小区名称" + '</option>';
					for(var i = 0; i < bb.length; i++) {
						str += "<option value=" + bb[i].areaid + ">" + bb[i].areaname + "</option>";
					}
					$("#select_xiaoqu").html("");
					$("#select_xiaoqu").append(str);
				} else {
					alert(msg.resoponseDisp)
				}
			},
			error: function() {
				alert("获取信息失败");
			}
		});
	}

}
//物业改变查小区
$("#ispayment").change(function() {
	var ispayment = $("#ispayment").val();
	if(ispayment == 2) {
		$('.ispayment_show').show();
	} else {
		$('.ispayment_show').hide();
	}
})
//加载数据
function ajax_data(data) {
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectStatisticsListForPayKind',
		data: data,
		async: false,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				var bb = JSON.parse(msg.returnString);
				$("#count").html(msg.count);
				$("#bi_zong").html(bb.count);
				$("#bi_money").html(bb.paymentamt);

				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: 1,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}

$("#bp-3-element-test").on("click", function() {
	var aa = $(this).children(".active").children("a").html();
	var pageNumber = aa;
	var pageSize = "10";

	var merchantid = $("#select_wuye").val();
	var areaid = $("#select_xiaoqu").val();

	var star_time = $('.input_1').val();
	var end_time = $('.input_2').val();
	var paykind = $("#select_type2").val();
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
			startday: star_time,
			endday: end_time,
			merchantid: merchantid,
			areaid: areaid,
			paykind: paykind
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);

	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectStatisticsListForPayKind',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				console.log(msg);
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: aa,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});

})

function createShowingTable(bb) {
	//获取后台传过来的jsonData,并进行解析  
	//此处需要让其动态的生成一个table并填充数据 
	if(bb.total == 0) {
		$("#tbbb").html("");
	} else {

		var bb = JSON.parse(bb);
		console.log(bb);
		var tableStr = "";
		var len = bb.list.length;

		if(len == 0) {
			var wuxiaoxi = '<p>' + "暂无内容" + '</p>'
			$("#tbbb").html(wuxiaoxi);
		} else {
			for(var i = 0; i < len; i++) {
				//				0 未支付 1 已支付 2 已取消
				//				3 已退款 4 缴费失败， 已退款
				//				if(bb[i].username == undefined) {
				//					bb[i].username = '';
				//				}
				//
				//				if(bb[i].apipayid == undefined) {
				//					bb[i].apipayid = ""
				//				}
				var num = parseInt(i) + parseInt(1);
				tableStr = tableStr + '<tr>' +
					'<th>' + num + '</th>' +
					'<th>' + bb.list[i].merchantname + '</th>' +
					'<th>' + bb.list[i].areaname + '</th>' +
					'<th>' + bb.list[i].paykindname + '</th>' +
					'<th>' + bb.list[i].count + '</th>' +
					'<th>' + bb.list[i].paymentamt + '</th>' +

					'</tr>';
			}
			//将动态生成的table添加的事先隐藏的div中.  
			$("#tbbb").html(tableStr);

		}

	}
}

//模糊查询
$('#search_chaxun').click(function() {

	var merchantid = $("#select_wuye").val();
	var areaid = $("#select_xiaoqu").val();
	var pageNumber = "1";
	var pageSize = "10";
	var star_time = $('.input_1').val();
	var end_time = $('.input_2').val();
	var paykind = $("#select_type2").val();
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
			startday: star_time,
			endday: end_time,
			merchantid: merchantid,
			areaid: areaid,
			paykind: paykind
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	ajax_data(req3)

})

function datePickerInit() {
	var now = new Date();
	var aa = now.setDate(now.getDate() - 1);
	$('#datetimepicker1').datetimepicker({
		language: 'zh-CN',
		format: 'yyyy-mm-dd',
		startView: 'month',
		minView: 'month',
		todayBtn: 1,
		autoclose: 1,
	});
	$('#datetimepicker1').datetimepicker()
		.on('hide', function(ev) {
			var value = $("#datetimepicker1").val();
			$('#datetimepicker2').datetimepicker('setStartDate', value);
			$("#datetimepicker2").val('')
		});
	$('#datetimepicker1').datetimepicker('setEndDate', new Date(aa));

	$('#datetimepicker2').datetimepicker({
		language: 'zh-CN',
		format: 'yyyy-mm-dd',
		startView: 'month',
		minView: 'month',
		todayBtn: 1,
		autoclose: 1,
	});
	$('#datetimepicker2').datetimepicker('setEndDate', new Date(aa));
};

//确认对账
$(".sure_payment").click(function() {
	var ispayment = $("#ispayment").val();
	var file = urlo;
	var startday = $("#datetimepicker1").val();
	var endday = $("#datetimepicker2").val();
	var checkobject = "";
	if(ispayment == 0) {
		$("#tips").html("请先选择是否代收代付");
		$('#myModal_tips').modal('show');
		return
	}
	if(ispayment == 1) {
		checkobject = 'default'
	}
	if(ispayment == 2) {
		checkobject = $("#select_xiaoqu1").val();
		if(checkobject == '') {
			$("#tips").html("请先选择物业和小区");
			$('#myModal_tips').modal('show');
			return
		}
	}
	if(startday == '') {
		$("#tips").html("请先选择开始时间");
		$('#myModal_tips').modal('show');
		return
	}
	if(endday == '') {
		$("#tips").html("请先选择结束时间");
		$('#myModal_tips').modal('show');
		return
	}
	if(urlo == '') {
		$("#tips").html("请先上传对账文件");
		$('#myModal_tips').modal('show');
		return
	}
	$("#sure_payment").attr('disabled', true);	
	var data = JSON.stringify({
		userId: userId,
		fullname: fullname,
		requestString: JSON.stringify({
			fileurl: urlo,
			startday: startday,
			endday: endday,
			checkobject: checkobject,
		})
	})
	var url_info = urltest + 'verify/financial/accountChecking';
	var oReq = new XMLHttpRequest();
	oReq.open("POST", url_info, true);
	oReq.setRequestHeader("Content-type", "application/json;charset=utf-8;");
	oReq.responseType = "blob";
	oReq.onload = function(oEvent) {
		if(oEvent.currentTarget.status == 200 || oEvent.status == 200) {	
			$("#sure_payment").attr('disabled', false);
			var content = oReq.response;
			var elink = document.createElement('a');
			elink.download = "对账结果.xls";
			elink.style.display = 'none';
			var blob = new Blob([content]);
			elink.href = URL.createObjectURL(blob);
			document.body.appendChild(elink);
			elink.click();
			document.body.removeChild(elink);
		} else {
			alert("错误")
		}
	};
	oReq.send(data);
	$("#sure_payment").attr('disabled', false);
});